<template>
  <div class="app-container" v-loading="pageLoading">
    <el-form
      :model="queryParams"
      ref="queryForm"
      size="small"
      :inline="true"
      v-show="showSearch"
      label-width="68px"
    >
      <el-form-item label="报告名称" prop="reportName">
        <el-input
          v-model="queryParams.reportName"
          placeholder="请输入报告名称"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item>
        <el-button
          type="primary"
          icon="el-icon-search"
          size="mini"
          @click="handleQuery"
          >搜索</el-button
        >
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery"
          >重置</el-button
        >
        <el-button size="mini" @click="batchDownload">批量下载</el-button>
      </el-form-item>
    </el-form>

    <el-table
      v-loading="loading"
      :data="reportList"
      @selection-change="handleSelectionChange"
    >
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="家人姓名" align="center" prop="familyName" />

      <el-table-column label="设备类型" align="center" prop="deviceType">
        <template slot-scope="scope">
          <div>{{ getTypeName(scope.row.deviceType) }}</div>
        </template>
      </el-table-column>

      <el-table-column label="报告名称" align="center" prop="reportName" />
      <el-table-column label="创建时间" align="center" prop="createTime" />

      <el-table-column label="状态" align="center" prop="status">
        <template slot-scope="scope">
          <div>
            {{
              scope.row.status === "1"
                ? "待审核"
                : scope.row.status === "2"
                ? "待推送"
                : scope.row.status === "3"
                ? "已推送"
                : "忽略"
            }}
          </div>
        </template>
      </el-table-column>

      <el-table-column label="随访状态" align="center" prop="followStatus">
        <template slot-scope="scope">
          <div>
            {{
              scope.row.followStatus === "0"
                ? "未随访"
                : scope.row.followStatus === "1"
                ? "随访中"
                : scope.row.followStatus === "2"
                ? "已完成"
                : ""
            }}
          </div>
        </template>
      </el-table-column>

      <el-table-column
        label="创建时间"
        align="center"
        prop="createTime"
        width="180"
      >
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.createTime) }}</span>
        </template>
      </el-table-column>
      <el-table-column
        label="操作"
        align="center"
        class-name="small-padding fixed-width"
      >
        <template slot-scope="scope">
          <el-button
            v-if="scope.row.status === '1'"
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate1(scope.row)"
            v-hasPermi="['qiushi:equipment:qiushreport:edit']"
            >审核
          </el-button>
          <el-button
            v-if="scope.row.status === '2'"
            size="medium"
            type="primary"
            @click="handleUpdate2(scope.row)"
            v-hasPermi="['qiushi:equipment:qiushreport:edit']"
            >推送
          </el-button>
          <el-button
            size="medium"
            type="primary"
            :loading="ylLoading"
            @click="handleOpenReport(scope.row)"
            >预览
          </el-button>
          <el-button
            v-if="scope.row.followStatus === '0'"
            size="medium"
            type="primary"
            @click="handleSend(scope.row)"
            v-hasPermi="['qiushi:equipment:qiushreport:handleSend']"
            >随访
          </el-button>
          <el-button
            v-if="scope.row.followStatus === '1'"
            size="medium"
            type="primary"
            @click="handleSend1(scope.row)"
            v-hasPermi="['qiushi:equipment:qiushreport:edit']"
            >随访完成
          </el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total > 0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改预警对话框 -->
    <el-dialog
      :title="title"
      :visible.sync="open1"
      width="500px"
      append-to-body
    >
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="家人姓名" prop="familyName">
          <div>{{ form.familyName }}</div>
        </el-form-item>
        <el-form-item label="设备类型" prop="deviceType">
          <div>{{ getTypeName(form.deviceType) }}</div>
        </el-form-item>
        <el-form-item label="报告名称" prop="reportName">
          <div>{{ form.reportName }}</div>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="handle2">通 过</el-button>
        <el-button @click="handle4">忽 略</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>

    <!-- 添加或修改预警对话框 -->
    <el-dialog
      :title="title"
      :visible.sync="open2"
      width="500px"
      append-to-body
    >
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <div>是否推送给{{ form.familyName }}?</div>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm1">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
    <!-- 预览报表 -->
    <el-dialog
      title=""
      :visible.sync="openReportVisible"
      width="800px"
      append-to-body
    >
      <PdfToReport
        :reportInfo="reportInfo"
        @resetPdfToSave="resetPdfToSave"
        domId="pdfDom"
        chartId="chartId"
      />
    </el-dialog>

    <!-- 添加或修改预警对话框 -->
    <el-dialog
      :title="title"
      :visible.sync="open3"
      width="500px"
      append-to-body
    >
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="问卷" prop="type">
          <el-select v-model="form.formId" placeholder="请选择问题问卷">
            <el-option
              v-for="item in formList"
              :key="item.id"
              :label="item.name"
              :value="item.id"
            >
            </el-option>
          </el-select>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="handle5">发 送</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>

    <!-- 添加或修改预警对话框 -->
    <el-dialog
      :title="title"
      :visible.sync="open6"
      width="500px"
      append-to-body
    >
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <h1>是否完成{{ form.familyName }}的随访?</h1>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="handle6">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
    <div style="width: 794px; position: absolute; left: -999999px; top: -999px">
      <template v-for="(item, index) in selectList">
        <div>
          <PdfToReport
            :reportInfo="JSON.parse(item.reportData)"
            @resetPdfToSave="resetPdfToSave"
            :domId="'pdfDom' + item.id"
            :chartId="'chartId' + +item.id"
          />
        </div>
      </template>
    </div>
  </div>
</template>

<script>
import {
  listReport,
  getReport,
  delReport,
  addReport,
  updateReport,
  send,
} from "@/api/qiushi/equipment/qiushreport";

import { uploadPDF } from "../../../api/login";
import { listForm } from "@/api/qiushi/followup/qiushiform";
import { listType } from "@/api/qiushi/equipment/qiushidevicetype";
import PdfToReport from "./components/pdfToReport";
export default {
  name: "Report",
  components: {
    PdfToReport,
  },
  data() {
    return {
      selectList: [],
      pageLoading: false,
      // 字典
      status_dict: [
        {
          value: "1",
          label: "待审核",
        },
        {
          value: "2",
          label: "通过",
        },
        {
          value: "4",
          label: "忽略",
        },
      ],
      reportInfo: null,
      ylLoading: false,
      // 遮罩层
      loading: true,
      textarea: "",
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 报告表格数据
      reportList: [],
      deviceList: [],
      formList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open1: false,
      open2: false,
      open3: false,
      open4: false,
      open5: false,
      open6: false,
      openReportVisible: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        familyId: null,
        deviceType: null,
        reportName: null,
        reportData: null,
        reportUrl: null,
        status: null,
        auditTime: null,
        auditUserId: null,
        auditUserName: null,
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        familyId: [
          {
            required: true,
            message: "家人id  member_family.id不能为空",
            trigger: "blur",
          },
        ],
        // reportName: [
        //   { required: true, message: "报告名称不能为空", trigger: "blur" },
        // ],
        // delFlag: [
        //   { required: true, message: "0正常 1删除不能为空", trigger: "blur" },
        // ],
        // createTime: [
        //   { required: true, message: "创建时间不能为空", trigger: "blur" },
        // ],
        // updateTime: [
        //   { required: true, message: "更新时间不能为空", trigger: "blur" },
        // ],
      },
    };
  },

  created() {
    this.getList();
  },
  mounted() {},

  methods: {
    async batchDownload() {
      if (this.selectList.length <= 0) {
        this.$modal.msgWarning("请选择报告");
        return;
      }
      this.pageLoading = true;
      for (let index = 0; index < this.selectList.length; index++) {
        const item = this.selectList[index];
        const PDF = await this.getPdf("pdfDom" + item.id);
        PDF.save(JSON.parse(item.reportData).title + "血压监测报告" + ".pdf");
      }
      this.pageLoading = false;
    },
    getTypeName(id) {
      for (let i = 0; i < this.deviceList.length; i++) {
        if (this.deviceList[i].id === id) return this.deviceList[i].name;
      }
    },
    getFormName(id) {
      for (let i = 0; i < this.formList.length; i++) {
        if (this.formList[i].id === id) return this.formList[i].name;
      }
    },

    handle2() {
      this.form.status = "2";
      this.submitForm();
    },
    handle3() {
      this.form.status = "3";
      this.submitForm();
    },
    handle4() {
      this.form.status = "4";
      this.submitForm();
    },
    handle5() {
      this.$refs["form"].validate((valid) => {
        if (valid) {
          this.form.formName = this.getFormName(this.form.formId);
          if (this.form.id != null) {
            this.form.reportData = null;

            send(this.form).then((response) => {
              this.$modal.msgSuccess("发送成功");
              this.open1 = false;
              this.open2 = false;
              this.open3 = false;
              this.getList();
            });
          }
        }
      });
    },
    handle6() {
      this.$refs["form"].validate((valid) => {
        if (valid) {
          if (this.form.id != null) {
            this.form.followStatus = "2";

            this.form.reportData = null;

            updateReport(this.form).then((response) => {
              this.$modal.msgSuccess("修改成功");
              this.open1 = false;
              this.open2 = false;
              this.open3 = false;
              this.open4 = false;
              this.open5 = false;
              this.open6 = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 查询报告列表 */
    getList() {
      this.loading = true;

      listType().then((response) => {
        this.deviceList = response.rows;
      });

      listForm({}).then((response) => {
        this.formList = response.rows;
      });

      listReport(this.queryParams).then((response) => {
        this.reportList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    handleSend(row) {
      this.reset();
      const id = row.id || this.ids;
      getReport(id).then((response) => {
        this.form = response.data;
        this.open3 = true;
        this.title = "发送随访信息";
      });
    },
    handleSend1(row) {
      this.reset();
      const id = row.id || this.ids;
      getReport(id).then((response) => {
        this.form = response.data;
        this.open6 = true;
        this.title = "";
      });
    },
    // 取消按钮
    cancel() {
      this.open1 = false;
      this.open2 = false;
      this.open3 = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        familyId: null,
        deviceType: null,
        reportName: null,
        reportData: null,
        reportUrl: null,
        status: null,
        auditTime: null,
        auditUserId: null,
        auditUserName: null,
        delFlag: null,
        createBy: null,
        createTime: null,
        updateBy: null,
        updateTime: null,
        followStatus: null,

        familyName: null,
      };
      this.resetForm("form");
    },
    resetPdfToSave() {
      this.getList();
      this.openReportVisible = false;
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.selectList = selection;
      this.ids = selection.map((item) => item.id);
      this.single = selection.length !== 1;
      this.multiple = !selection.length;
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open1 = true;
      this.title = "添加报告";
    },
    /** 修改按钮操作 */
    handleUpdate1(row) {
      this.reset();
      const id = row.id || this.ids;
      getReport(id).then((response) => {
        this.form = response.data;
        this.open1 = true;
        this.title = "修改报告";
      });
    },
    handleUpdate2(row) {
      this.reset();
      const id = row.id || this.ids;
      getReport(id).then((response) => {
        this.form = response.data;
        this.open2 = true;
        this.title = "修改报告";
      });
    },
    handleOpenReport(row) {
      const id = row.id || this.ids;
      console.log(id, "13");
      // this.reportInfo = id;
      getReport(id).then((response) => {
        if (JSON.parse(response.data.reportData)) {
          this.reportInfo = JSON.parse(response.data.reportData);
          this.reportInfo.id = response.data.id;
          this.openReportVisible = true;
        } else {
          this.$modal.msgWarning("暂无数据");
        }
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate((valid) => {
        if (valid) {
          if (this.form.id != null) {
            updateReport(this.form).then((response) => {
              this.$modal.msgSuccess("修改成功");
              this.open1 = false;
              this.getList();
            });
          } else {
            addReport(this.form).then((response) => {
              this.$modal.msgSuccess("新增成功");
              this.open1 = false;
              this.getList();
            });
          }
        }
      });
    },
    submitForm1() {
      this.$refs["form"].validate((valid) => {
        if (valid) {
          if (this.form.id != null) {
            this.form.status = "3";

            updateReport(this.form).then((response) => {
              this.$modal.msgSuccess("修改成功");
              this.open1 = false;
              this.open2 = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$modal
        .confirm('是否确认删除报告编号为"' + ids + '"的数据项？')
        .then(function () {
          return delReport(ids);
        })
        .then(() => {
          this.getList();
          this.$modal.msgSuccess("删除成功");
        })
        .catch(() => {});
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download(
        "qiushi/equipment/qiushreport/export",
        {
          ...this.queryParams,
        },
        `report_${new Date().getTime()}.xlsx`
      );
    },
  },
};
</script>
<style lang="scss" scoped>
.header {
  text-align: center;

  .title {
    font-size: 26px;
  }
  .subTitle {
    margin: 10px 0;
    font-size: 22px;
  }
}
.table {
  border: 1px solid #000;
  .firstLine {
    display: flex;
    justify-content: center;
    border-bottom: 1px solid #000;
    .item {
      width: 33.33%;
      text-align: center;
      & > div {
        margin: 6px 0;
      }
    }
  }
  .secndLine {
    display: flex;
    justify-content: space-between;
    align-items: center;
    background: #dbecf4;
    font-size: 20px;
    padding: 8px 10px;

    .left {
      font-size: 18px;
    }
    .right {
      font-size: 16px;
      span {
        font-weight: bold;
      }
    }
  }
  .tableBox {
    padding: 8px;
    margin-top: 20px;
    .tableTitle {
      margin-top: 5px;
      position: relative;
      padding-left: 10px;
      margin-bottom: 10px;
      &::before {
        content: "";
        position: absolute;
        left: 0;
        top: -4px;
        height: 25px;
        width: 3px;
        background: #004d99;
      }
    }
    .tableInfo {
      .th,
      .tr {
        text-align: center;
        background: #f0f0f0;
        display: flex;
        justify-content: space-between;
        border-top: 1px solid #000;
        font-size: 15px;
        font-family: PingFangSC, PingFang SC;
        font-weight: 500;
        color: #333333;
        padding: 10px 0;
        & > div {
          width: 25%;
        }
      }
      .tr {
        background: #fff;
      }
    }
    .info {
      display: flex;
      border-bottom: 1px dashed #d3d3d3;
      padding-bottom: 5px;
      font-size: 16px;
      font-family: PingFangSC, PingFang SC;
      font-weight: 500;
      color: #333333;
      line-height: 22px;
      margin-top: 20px;
      & > div {
        margin-right: 20px;
      }
    }
  }
  .blueTableBox {
    padding: 10px;
    .tableInfo {
      border: 1px solid #ebeef5;
      color: #000;
      text-align: center;
    }
    .th {
      display: flex;
      align-items: center;
      background: #57a0d4;
      padding: 10px 0;
      & > div {
        border-right: 1px solid #ebeef5;
      }
    }
    .tr {
      display: flex;
      padding: 10px 0;
      align-items: center;
      & > div {
        border-right: 1px solid #ebeef5;
        &:last-of-type {
          border: none;
        }
      }
    }
  }
  .lineChat {
    height: 400px;
  }
}
</style>
